<?php

namespace app\controllers\h5;

use app\controllers\h5\base\H5Controller;
use app\models\product\Product;
use app\models\product\ProductCategory as Model;
use app\controllers\h5\services\Category as Service;
use app\redis\Cache;

class CategoryController extends H5Controller
{
    /**
     * all categories
     * (3 level)
     *
     * 这个目前 没有使用
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionCategories()
    {
        $cache = Cache::get(Cache::$CATEGORIES);
        if (!$cache) {
            $list = Model::find()
                ->where(['parent_id' => 0, 'status' => Model::$statusMapping['OK']])
                ->select(['id', 'name', 'image_id', 'parent_id'])
                ->with(['children', 'children.children'])
                ->orderBy(['sort' => SORT_ASC, 'id' => SORT_ASC])
                ->asArray()
                ->all();
            Cache::set(Cache::$CATEGORIES, json_encode($list));
        } else {
            $list = json_decode($cache, true);
        }

        return $this->successEncode($list);
    }

    /**
     * children list (include 3 level)
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionCategoryByParent()
    {
        $parent_id = intval($this->post('parent_id', 0));


        // is show name
        $name = boolval($this->post('name'));
        if ($name) {
            $name = Model::find()
                ->where(['id' => $parent_id])
                ->select(['name'])
                ->scalar();
        }

        $list = Service::getChildrenByParent($parent_id);

        return $name
            ? $this->successEncode(['list' => $list, 'name' => $name])
            : $this->successEncode($list);
    }

    /**
     * product list by category
     *
     * @return array|string[]|\yii\web\Response
     */
    public function actionProductByCategory()
    {
        $category_id = intval($this->post('category_id', 0));

        $page = intval($this->post('page', $this->page));
        $pageSize = intval($this->post('pageSize', $this->pageSize));

        $key = Cache::$CATEGORIES_PRODUCT . $category_id . '-' . $page;
        $cache = Cache::get($key);

        if (!$cache || true) {
            $title = Model::find()
                ->where(['id' => $category_id])
                ->select(['name'])
                ->scalar();

            $query = Product::find()
                ->where(['status' => Product::$statusMapping['OK']])
                ->andWhere(['category_id' => $category_id]);

            $totalCount = $query->count();
            $totalPage = ceil($totalCount / $pageSize);

            $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
                ->offset(($page - 1) * $pageSize)
                ->limit($pageSize)
                ->with(['mainImage', 'scoreCost'])
                ->asArray()
                ->all();

            Cache::set($key, json_encode([
                'title' => $title,
                'totalCount' => $totalCount,
                'totalPage' => $totalPage,
                'list' => $list
            ]));
        } else {
            $data = json_decode($cache, true);
            $title = $data['title'];
            $totalCount = $data['totalCount'];
            $totalPage = $data['totalPage'];
            $list = $data['list'];
        }

        return $this->successEncode([
            'title' => $title,
            $this->list => $list,
            $this->totalCount => (int)$totalCount,
            $this->totalPage => $totalPage,
            $this->currentPage => $page
        ]);
    }

    // 一级分类的产品
    public function actionOneProduct()
    {
        $category_id = intval($this->post('category_id', 0));

        $query = Product::find()
            ->where(['status' => Product::$statusMapping['OK']])
            ->andWhere(['category_id' => $category_id]);

        $list = $query->orderBy(['sort' => SORT_ASC, 'id' => SORT_DESC])
            ->with(['mainImage', 'scoreCost'])
            ->asArray()
            ->all();

        return $this->successEncode(['list' => $list]);
    }
}